package com.timberwood.model.dao;


import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;


import com.timberwood.model.entity.Products;
import com.timberwood.util.HibernateUtil;

public class ProductDAO {
	
	private Session session;
	private Transaction transaction;
	
	public ProductDAO() {
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		transaction = session.beginTransaction();
	}
	
	
	//method lấy về 6 sản phẩm có số lượng bán lớn nhất
	@SuppressWarnings("unchecked")
	public List<Products> sleSalestProducts(){
		List<Products> sellersProducts=null;
		try {
			sellersProducts=(List<Products>)session.getNamedQuery("sleSalestProduct").setMaxResults(6).list();
		} catch (HibernateException he) {
			transaction.rollback();
		}
		transaction.commit();
		return sellersProducts;
	}
	
	
	//method lấy về 6 sản phẩm có mã lớn nhất
	@SuppressWarnings("unchecked")
	public List<Products> sleNewsProducts(){
		List<Products> newsProducts=null;
		try {
			newsProducts=(List<Products>)session.getNamedQuery("sleNewsProduct").setMaxResults(6).list();
		} catch (HibernateException he) {
			transaction.rollback();
		}
		transaction.commit();
		return newsProducts;
	}
	
	
	//method lấy về tất cả các sản phẩm có số cùng loại
	@SuppressWarnings("unchecked")
	public List<Products> sleProductsByCateId(int cateId){
		List<Products> productsByCate=null;
		try {
			productsByCate=(List<Products>)session.getNamedQuery("sleProductByCateId").setInteger("cateId", cateId).list();
		} catch (HibernateException	he) {
			transaction.rollback();
			he.printStackTrace();
		}
		transaction.commit();
		return productsByCate;
	}
	
	//method lấy về sản phẩm theo mã sản phẩm
	public Products productById(int proId) {
		Products products=null;
		try {
			products=(Products) session.getNamedQuery("sleProductById").setInteger("proId", proId).uniqueResult();
		} catch (HibernateException he) {
			transaction.rollback();
			he.printStackTrace();
		}
		transaction.commit();
		return products;
	}
	
	
	//method cập nhật sản phẩm
	public boolean updateProduct(Products objProduct){
		boolean status;
		try {
			session.update(objProduct);
			status=true;
		} catch (HibernateException he) {
			status=false;
			transaction.rollback();
			he.printStackTrace();
		}
		return status;
	}
	
	//method thêm mới sản phẩm
	public boolean insertProduct(Products objProduct){
		boolean status;
		try {
			session.update(objProduct);
			status=true;
		} catch (HibernateException he) {
			status=false;
			transaction.rollback();
			he.printStackTrace();
		}
		return status;
	}
	
	//method xóa sản phẩm theo mã sản phẩm
	public boolean deleteProduct(int proId) {
		boolean status;
		try {
		session.getNamedQuery("deleteproduct").setInteger("proId", proId).executeUpdate();
			status=true;
		} catch (HibernateException he) {
			status=false;
			transaction.rollback();
			he.printStackTrace();
		}
		return status;
	}
	
	
	
	//method lấy về tất cả các sản phẩm đang bán
	@SuppressWarnings("unchecked")
	public List<Products> proBusyA(){
		List<Products> listProduct=null;
		try {
			listProduct=(List<Products>)session.getNamedQuery("sleProBusyA").list();
		} catch (HibernateException he) {
			transaction.rollback();
		}
		transaction.commit();
		return listProduct;
	}
	
	//method lấy về tất cả các sản phẩm ngừng bán
	@SuppressWarnings("unchecked")
	public List<Products> proStopSellingA(){
		List<Products> listProduct=null;
		try {
			listProduct=(List<Products>)session.getNamedQuery("sleProStopSellingA").list();
		} catch (HibernateException he) {
			transaction.rollback();
		}
		transaction.commit();
		return listProduct;
	}
	
	
		
}
